package com.engc.common.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.engc.common.dao.BaseDao;
import com.engc.common.dao.DBExecuteException;
import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;
import com.engc.common.utils.FilterGroup;
import com.engc.common.utils.FilterTranslator;

public class BaseServiceImpl implements BaseService {
	private BaseDao baseDao;

	@Override
	public int Insert(String tableName, Object pojo) throws Exception {
		return baseDao.Insert(tableName, pojo);
	}

	@Override
	public int Insert(String tableName, Object pojo, String... column) throws Exception {
		return baseDao.Insert(tableName, pojo, column);
	}

	@Override
	public int Insert(String tableName, Map<String, Object> record) throws Exception {
		return baseDao.Insert(tableName, record);
	}

	@Override
	public int InsertBySql(String sql, Object... parameter) throws Exception {
		return baseDao.Insert(sql, parameter);
	}

	@Override
	public int Update(String tableName, Object pojo, String where, Object... parameter) throws Exception {
		return baseDao.Update(tableName, pojo, where, parameter);
	}

	@Override
	public int Update(String tableName, Object pojo, String where, String[] column, Object... parameter) throws Exception {
		return baseDao.Update(tableName, pojo, where, column, parameter);
	}

	@Override
	public int Update(String tableName, Map<String, Object> record, String where, Object... parameter) throws Exception {
		return baseDao.Update(tableName, record, where, parameter);
	}

	@Override
	public int Delete(String table, String where, Object... parameter) throws Exception {
		return baseDao.Delete(table, where, parameter);
	}

	@Override
	public int Delete(String sql, Object... parameter) throws Exception {
		return baseDao.Delete(sql, parameter);
	}

	@Override
	public Map<String, Object> Read(String sql, Object... parameter) {
		return baseDao.Read(sql, parameter);
	}

	@Override
	public <T> T Read(Class<T> clazz, String sql, Object... parameter) {
		return baseDao.Read(clazz, sql, parameter);
	}

	@Override
	public List<Map<String, Object>> Query(String sql, Object... parameter) {
		return baseDao.Query(sql, parameter);
	}

	@Override
	public <T> List<T> Query(Class<T> clazz, String sql, Object... parameter) {
		return baseDao.Query(clazz, sql, parameter);
	}

	@Override
	public <T> List<T> Query_SliceOracle(Class<T> beanClass, String sql, int page, int pagesize, Object... params) {
		return baseDao.Query_SliceOracle(beanClass, sql, page, pagesize, params);
	}

	@Override
	public <T> List<T> Query_SliceMySql(Class<T> beanClass, String sql, int page, int pagesize, Object... pars) {
		return baseDao.Query_SliceMySql(beanClass, sql, page, pagesize, pars);
	}

	@Override
	public List<Map<String, Object>> Query_SliceOracle(String sql, int page, int pagesize, Object... parameter) {
		return baseDao.Query_SliceOracle(sql, page, pagesize, parameter);
	}

	@Override
	public List<Map<String, Object>> Query_SliceMySql(String sql, int page, int pagesize, Object... parameter) {
		return baseDao.Query_SliceMySql(sql, page, pagesize, parameter);
	}

	@Override
	public Map<String, Object> Read_Cache(String cacheName, Serializable key, String sql, Object... params) {
		return baseDao.Read_Cache(cacheName, key, sql, params);
	}

	@Override
	public <T> T Read_Cache(Class<T> beanClass, String cacheName, Serializable key, String sql, Object... params) {
		return baseDao.Read_Cache(beanClass, cacheName, key, sql, params);
	}

	@Override
	public List<Map<String, Object>> Query_Cache(String cacheName, Serializable key, String sql, Object... params) {
		return baseDao.Query_Cache(cacheName, key, sql, params);
	}

	@Override
	public <T> List<T> Query_Cache(Class<T> beanClass, String cacheName, Serializable key, String sql, Object... params) {
		return baseDao.Query_Cache(beanClass, cacheName, key, sql, params);
	}

	@Override
	public int execute(String sql, Object... pars) throws Exception {
		return baseDao.execute(sql, pars);
	}

	@Override
	public int Update(String sql, Object... parameter) throws Exception {
		return baseDao.Update(sql, parameter);
	}

	@Override
	public void saveOrUpdate(String tableName, Map<String, Object> record,
			String where, Object... parameter) throws Exception {
		int count = baseDao.getTotal(tableName, where, parameter);
		if(count == 0){
			baseDao.Insert(tableName, record);
		}else{
			baseDao.Update(tableName, record, where, parameter);
		}
	}

	@Override
	public int getTotal(String tableName, String sWhere, Object... params) {
		return baseDao.getTotal(tableName, sWhere, params);
	}

	@Override
	public int getTotal(String sql, Object... parameter) {
		return baseDao.getTotal(sql, parameter);
	}
	
	
	public  int executeCountSql(String sql , Object... parameter){
		return baseDao.executeCountSql(sql, parameter);
	}
	

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public String getSeqValue(String seqName) {
		return baseDao.getSeqValue(seqName);
	}

	@Override
	public int logicDelete(String tableName, String where, Object... params) throws Exception {
		Map<String, Object> record = new HashMap<String, Object>();
		record.put("del_flag", 0);
		return baseDao.Update(tableName, record, where, params);
	}

	@Override
	public void addOperationLog(String tableName, String id, String operuser, int action, String remark) throws Exception {
		String sql = "insert into SYS_OPERATING_RECORD (TABLENAME,PKID,OPER_USER,OPER_TIME,ACTION,REMARK)values(?,?,?,sysdate,?,?)";
		baseDao.Insert(sql, tableName, id, operuser, action, remark);
	}

	@Override
	public Map<String, Object> getOperationLogInfoByAdd(String tableName, String id) {
		String sql = "select  * from SYS_OPERATING_RECORD where tablename=? and pkid=? and action=?";
		return baseDao.Read(sql, tableName, id, BaseService.actionAdd);
	}

	@Override
	public Map<String, Object> getOperationLogInfoByLastModify(String tableName, String id) {
		String sql = "select  * from SYS_OPERATING_RECORD where tablename=? and pkid=? and action=? order by oper_time desc";
		List<Map<String, Object>> records = baseDao.Query(sql, tableName, id, BaseService.actionModify);
		return records.get(0);
	}

	@Override
	public Map<String, Object> getOperationLogInfoByApply(String tableName, String id) {
		String sql = "select  * from SYS_OPERATING_RECORD where tablename=? and pkid=? and action=?";
		return baseDao.Read(sql, tableName, id, BaseService.actionApply);
	}

	@Override
	public Map<String, Object> getGridData(String Sql, String alien, HttpServletRequest req) {
		String sortname = req.getParameter("sortname");
		String sortorder = req.getParameter("sortorder");
		String _pagenumber = req.getParameter("page");
		String _pagesize = req.getParameter("pagesize");
		String where = req.getParameter("where");
		int pagenumber = 1, pagesize = 0;
		// 可分页
		if (!BaseUtils.isEmpty(_pagenumber) && !BaseUtils.isEmpty(_pagesize)) {
			pagenumber = Integer.valueOf(_pagenumber);
			pagesize = Integer.valueOf(_pagesize);
			if (pagesize == 0)
				pagesize = 20;
		}

		String whereSql = " 1=1 ";
		Object[] parameter = null;
		if (where != null && !"".equals(where)) {
			FilterGroup wheres = JSON.parseObject(where, FilterGroup.class);
			FilterTranslator translator = new FilterTranslator(wheres);
			whereSql = translator.TranslateGroup(wheres, "".equals(alien) ? " " : " " + alien + ".");
			parameter = translator.getParameters();
		}
		StringBuffer sb = new StringBuffer();
		sb.append(Sql).append(" WHERE ").append(whereSql);
		if (!BaseUtils.isEmpty(sortname)) {
			sb.append(" ORDER BY ").append("".equals(alien) ? sortname : alien + "." + sortname).append(" ").append(sortorder);
		}
		String totalSql = "select count(1) from (" + Sql + " where " + whereSql + ")";
		int Total = baseDao.getTotal(totalSql, parameter);
		List<Map<String, Object>> gridData = baseDao.Query_SliceOracle(sb.toString(), pagenumber, pagesize, parameter);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Total", Total);
		map.put("Rows", gridData == null ? new ArrayList<Map<String, Object>>() : gridData);
		return map;
	}

	
	
}
